﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using mitasoft.AdsShedule.Common;

namespace mitasoft.AdsShedule.Forms
{
    public partial class MakeAppoiment : Form
    {

        public bool ExistModif { get; private set; }

        public MakeAppoiment()
        {
            InitializeComponent();
            this.Load += new EventHandler(MakeAppoiment_Load);
        }

        void MakeAppoiment_Load(object sender, EventArgs e)
        {
            PutToggleControls();
        }

        private void PutToggleControls()
        {
            foreach (var d in AppCommon.GetDaysInMonth())
            {
                daysContainerAM1.Controls.Add(CreateToggleButton(d, IntervalType.AM1));
                daysContainerAM2.Controls.Add(CreateToggleButton(d, IntervalType.AM2));
                daysContainerAM3.Controls.Add(CreateToggleButton(d, IntervalType.AM3));
                daysContainerPM1.Controls.Add(CreateToggleButton(d, IntervalType.PM1));
                daysContainerPM2.Controls.Add(CreateToggleButton(d, IntervalType.PM2));
            }
        }


        private CheckBox CreateToggleButton(DateTime buttonDate, IntervalType intervalType)
        {
            string text = buttonDate.Day.ToString();
            bool isSunday = buttonDate.DayOfWeek == DayOfWeek.Sunday;
            AppoimentButtonInfo btnInfo = Business.BusinessAccess.Instance.GetAppoimentButtonInfo(buttonDate, intervalType);

            var checkBox = new System.Windows.Forms.CheckBox();
            checkBox.MouseDown += new MouseEventHandler(checkBox_MouseDown);
            checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);

            checkBox.Appearance = System.Windows.Forms.Appearance.Button;
            checkBox.Text = text;
            checkBox.TextAlign = ContentAlignment.MiddleCenter;
            checkBox.Width = 30;
            checkBox.Height = 30;
            checkBox.Name = string.Format("ID{0}{1}", text, intervalType);
            checkBox.Enabled = !isSunday;
            checkBox.BackColor = isSunday ? Color.Red : Color.LightGreen;
            checkBox.Tag = btnInfo;
            checkBox.CheckState = btnInfo.VoiceAssocID.Count > 0 ? CheckState.Checked : CheckState.Unchecked;

            return checkBox;
        }

        #region ToggleButtons Events

        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            var b = (CheckBox)sender;

            if (!b.Checked)
            {
                b.BackColor = Color.LightGreen;
            }
            else
            {
                b.BackColor = Color.Yellow;
            }

        }
        private void checkBox_MouseDown(object sender, MouseEventArgs e)
        {
            IntervalType locationInterval = IntervalType.AM1;
            var bCB = (CheckBox)sender;

            switch (bCB.Parent.Name)
            {
                case "daysContainerAM1":
                    locationInterval = IntervalType.AM1;
                    break;

                case "daysContainerAM2":
                    locationInterval = IntervalType.AM2;
                    break;

                case "daysContainerAM3":
                    locationInterval = IntervalType.AM3;
                    break;

                case "daysContainerPM1":
                    locationInterval = IntervalType.PM1;
                    break;

                case "daysContainerPM2":
                    locationInterval = IntervalType.PM2;
                    break;
            }

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                Forms.DisplayInfo displayInfo = new DisplayInfo();
                displayInfo.InvervalType = locationInterval;
                displayInfo.ButtonInfo = (Common.AppoimentButtonInfo)bCB.Tag;
                displayInfo.ShowDialog();
            }
            else if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Forms.VoiceSelection voiceSelectionForm = new VoiceSelection();
                voiceSelectionForm.ButtonInfo = (AppoimentButtonInfo)bCB.Tag;

                ExistModif = ExistModif == false ? true : ExistModif;

                if (voiceSelectionForm.ShowDialog() == System.Windows.Forms.DialogResult.No)
                {
                    bCB.Checked = voiceSelectionForm.ButtonInfo.VoiceAssocID.Count > 0;
                    Business.BusinessAccess.Instance.SaveAppoimentButtonInfoToData(voiceSelectionForm.ButtonInfo);
                }
                else
                {
                    bCB.Checked = true;
                    Business.BusinessAccess.Instance.SaveAppoimentButtonInfoToData(voiceSelectionForm.ButtonInfo);
                }

                //switch (b.Parent.Name)
                //{
                //    case "daysContainerAM1":
                //        Save(IntervalType.AM1);
                //        break;

                //    case "daysContainerAM2":
                //        Save(IntervalType.AM2);
                //        break;

                //    case "daysContainerAM3":
                //        Save(IntervalType.AM3);
                //        break;

                //    case "daysContainerPM1":
                //        Save(IntervalType.PM1);
                //        break;

                //    case "daysContainerPM2":
                //        Save(IntervalType.PM2);
                //        break;

                //}
            }
        }

        #endregion

        private void btnSaveAppoiment_Click(object sender, EventArgs e)
        {
            Business.BusinessAccess.Instance.SaveDataSet();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            Business.BusinessAccess.Instance.SaveDataSet();
            this.Close();
        }


    }
}
